<template>
	<view class="content">
		<view class="list">
			<radio-group name="pay">
				<view class="pay_box" v-html="pay"></view>
				<!-- <view class="row b-b" v-for="(way,index) in payList" :key='index'>

					<label class="radio input" @click="check(index,way.paytypecode,way)">
						<radio value="r1" color="#CA342E" />
						<image src="/static/img/1/112.png" mode="aspectFit" class="pay-icon" v-if="way.paytypecode == 'alipay'"></image>
						<image src="/static/img/1/114.png" mode="aspectFit" class="pay-icon" v-if="way.paytypecode == 'weixin'"></image>
						<image src="/static/img/1/120.png" mode="aspectFit" class="pay-icon" v-if="way.paytypecode == 'storagecard'"></image>
						<text class="pay-name">{{way.paytypename}}</text>

					</label>

				</view> -->
				<!-- <view class="row b-b" >
						
					<label class="radio input"><radio value="r1" color="#CA342E" checked="true" />
						<image src="/static/img/1/114.png" mode="aspectFit" class="pay-icon"></image>
						<text class="pay-name">微信支付</text>
				
					</label>
					
				</view>
				<view class="row b-b" >
						
					<label class="radio input"><radio value="r1" color="#CA342E" />
						<image src="/static/img/1/116.png" mode="aspectFit" class="pay-icon"></image>
						<text class="pay-name">Apple Pay</text>
						<image src="/static/img/1/117.png" mode="aspectFit" class="other-pay"></image>
						<image src="/static/img/1/118.png" mode="aspectFit" class="quickpay"></image>
						<image src='/static/img/1/119.png' class="icon-y"></image>
					</label>
					
				</view>
				<view class="row b-b" >
						
					<label class="radio input"><radio value="r1" color="#CA342E" />
						<image src="/static/img/1/120.png" mode="aspectFit" class="pay-icon"></image>
						<text class="pay-name">云闪付</text>
						<image src="/static/img/1/117.png" mode="aspectFit" class="other-pay"></image>
					
						<image src='/static/img/1/119.png' class="icon-y"></image>
					</label>
					
				</view> -->
			</radio-group>

		</view>



		<!-- <button class="add-btn" @click="payorder">确定支付</button> -->

	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				checknum: '1',
				payList: [],
				payway: '',
				thisway: {},
				orderNo: '',
				pay: '',
				payMoney: ''
			}
		},
		onLoad(option) {
			let that = this
			this.orderNo = JSON.parse(option.data).cOrderNo
			this.$SysCache.put('orderNo', JSON.parse(option.data).cOrderNo, 864000)
			this.payMoney = JSON.parse(option.data).fPayMoney
			this.$SysCache.put('money', JSON.parse(option.data).fPayMoney, 864000)
			this.$Request.post(this.$api.httpApi.order.getEnablePayTypeList + "&token=" + this.$SysCache.get('token')).then(res => {
				if (res.code == 200) {
					this.payList = res.data
					this.payorder()
				}
			})
			
			
		},
		methods: {
			payorder(){
				let that = this
				let paydata = {
					"cOrderNo": that.orderNo,
					"paytype": 1,
					"remark": "营家支付",
					"paytypecode": "construct",
					"payBackUrl": "/client/Pay/payNotify?paytypecode=construct",
					// "trade_type": "JSAPI",
					// "oid": this.$SysCache.get('openid') //openid 
				}
				this.$Request.post(this.$api.httpApi.order.buildPayment + "&token=" + this.$SysCache.get('token'), paydata).then(
					res => {
						if (res.code == 200) {
							let typeData =''
							let openIdData = ''
							let ua = window.navigator.userAgent.toLowerCase()
							// 通过正则表达式匹配ua中是否含有MicroMessenger字符串
							if (ua.match(/MicroMessenger/i) == 'micromessenger') {
								console.log('微信浏览器')
								typeData = "CASHIER_PAY_JSAPI" // 微信浏览器
							} else{
								// alert(uni.getStorageSync('TYPE'))
								if(uni.getStorageSync('TYPE') ==='' ) {
									// alert("CASHIER_PAY_WAP")
									typeData = "CASHIER_PAY_WAP"
								} else {
									// alert("CASHIER_PAY_APP")
									typeData = "CASHIER_PAY_APP"
								}
								// if (uni.getStorageSync('nm').toString() === 'webKit,mobile') {
								// 	typeData = "CASHIER_PAY_WAP"
								// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,android') {
								// 	typeData = "CASHIER_PAY_WAP"
								// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,ios,android,iPhone,Safari'){
								// 	typeData = "CASHIER_PAY_APP"
								// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,android,Safari'){
								// 	typeData = "CASHIER_PAY_APP"
								// } else if(uni.getStorageSync('nm').toString() === 'webKit,mobile,ios,android,iPhone'){
								// 	typeData = "CASHIER_PAY_WAP"
								// }
							}
							console.log('payway', typeData)
							this.$Request.post4(
								this.$api.httpApi.user.pay, {
									"uid": this.$SysCache.get('userInfo').id||this.$SysCache.get('userInfo').mid,
									"pay_no": res.data.cPayNo,
									"order_no": res.data.order_no,
									// "page_url":`http://youyou.tangmu.xin/#/pages/order/success?cOrderNo=${that.orderNo}&type=1`,
									"page_url":`https://vip.inja.com/#/pages/order/success?cOrderNo=${that.orderNo}&type=1`,
									"amount": this.payMoney,
									"order_no":that.orderNo,
									"openId": this.$SysCache.get('openid')||null,
									"type":typeData
									
								}
							).then(res => {
								if (res.code == 1) {
									let ur = this.urlEncode(res.data)
									let xx = ur.substr(0,1)
									let yy = ur.replace(xx,'?')
									window.location.href = "https://ipay.inja.com/gateway/cashier/acquiring?dd=1"+ur  //正式环境
									// window.location.href = "http://ipay.ysyec.com/gateway/cashier/acquiring?dd=1"+ur //测试环境
									
							
								}
							})
						}
					})
			},
			urlEncode(param, key, encode) {
			  if(param==null) return '';
			  var paramStr = '';
			  var t = typeof (param);
			  if (t == 'string' || t == 'number' || t == 'boolean') {
			    paramStr += '&' + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);
			  } else {
			    for (var i in param) {
			      var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
			      paramStr += this.urlEncode(param[i], k, encode);
			    }
			  }
			  return paramStr;
			},
			check(str, code, way) {
				this.checknum = str
				this.payway = code
				this.thisway = way
				this.$SysCache.put('payway', code, 864000)
			},
			switchChange(e) {
				this.addressData.default = e.detail;
			},
			onBridgeReady(datas) {

				WeixinJSBridge.invoke(
					'getBrandWCPayRequest',
					datas,
					// {
					// 	"appId": datas.appId, //公众号名称，由商户传入     
					// 	"timeStamp": datas.timeStamp, //时间戳，自1970年以来的秒数     
					// 	"nonceStr": datas.nonceStr, //随机串     
					// 	"package": datas.package,
					// 	"signType": datas.signType, //微信签名方式：     
					// 	"paySign": datas.paySign //微信签名 

					// },
					function(res) {
						if (res.err_msg == "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							// uni.navigateTo({
							// 	url:`/pages/order/success?payway=${this.payway}&money=${this.payMoney}`
							// })
							window.location.href =
								`http://youyou.tangmu.xin/#/pages/order/success?payway=${this.payway}&money=${this.payMoney}`
						}
					});

				if (typeof WeixinJSBridge == "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
						document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
					}
				} else {
					// this.onBridgeReady(datas);
				}
			},
			tosuccess() {
				let that = this
				if (this.payway == '') {
					return
				}
				if (this.payway == 'weixin') {
					let paydata = {
						"cOrderNo": that.orderNo,
						"paytype": 1,
						"remark": "第三方支付",
						"paytypecode": "construct",
						"payBackUrl": "/client/Pay/payNotify?paytypecode=weixin",
						// "trade_type": "JSAPI",
						// "oid": this.$SysCache.get('openid') //openid 
					}
					this.$Request.post(this.$api.httpApi.order.buildPayment + "&token=" + this.$SysCache.get('token'), paydata).then(
						res => {
							if (res.code == 200) {
								// uni.request({
								// 	url: "http://47.107.92.235/company/yongyou/public/index.php/index/pays/paySign?PayWay=mp&openid=" + this.$SysCache
								// 		.get("openid") + "&token=" + that.$SysCache.get('token') + "&cOrderNo=" + that.orderNo,
								// 	method: "get",
								// 	success: function(res) {


								// 	},
								// 	fail: function(e) {

								// 	}
								// })
								that.onBridgeReady(JSON.parse(res.data))
								// console.log()
							}
						})





				}
				if (this.payway == 'storagecard') {
					// let paydata = {
					// 	"bizTransNo": that.orderNo,
					// 	"remark": this.thisway.description,
					// 	"orderAmt":that.payMoney,
					// 	"paytypecode": this.thisway.paytypecode,
					// 	"pageUrl": "/client/Pay/payNotify?paytypecode=weixin",
					// 	"trade_type": "JSAPI",
					// 	"oid": this.$SysCache.get('openid') //openid 
					// }
					let paydata = {
						"merchantNo": "815000401911",
						"transNo": "20200317103445",
						"orderAmt": "1",
						"requestDate": "20200317103445",
						"goodsInfo": "收银台测试",
						"additionalInfo": "原样返回信息",
						"bizTransNo": "20200317103445",
						"bizSystemType": "23123",
						"bizRequestDate": "20200317103445",
						"payeeName": "张三",
						"userType": "PERSON",
						"pageUrl": "",
						"returnUrl": ""
					}

					this.$Request.post5(this.$api.httpApi.user.pays + "&token=" + this.$SysCache.get('token'), paydata).then(res => {
						if (res.code == 200) {

							// that.onBridgeReady(JSON.parse(res.data))

						}
					})
				}


				// uni.navigateTo({
				// 	url:`/pages/order/success`
				// })
			}







		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;

	}

	.list {
		padding: 0 40upx;
		background: #fff;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		height: 135upx;

		radio {
			transform: scale(0.7)
		}

		// background: #fff;

		.tit {
			flex-shrink: 0;
			min-width: 120upx;
			font-size: 23upx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: rgba(102, 102, 102, 1);
		}

		.input {
			flex: 1;
			font-size: 23upx;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: rgba(153, 153, 153, 1);
			text-align: left;
			display: flex;
			justify-content: flex-start;
			align-items: center;

			.pay-icon {
				width: 56upx;
				height: 56upx;
				margin-left: 15upx;
			}

			.pay-name {
				font-size: 30upx;
				font-family: PingFang SC;
				font-weight: 500;
				color: rgba(43, 43, 43, 1);
				margin: 0 73upx 0 22upx;
			}

			.other-pay {
				width: 46upx;
				height: 34upx;
				position: absolute;
				right: 282upx;
			}

			.quickpay {
				width: 64upx;
				height: 34upx;
				position: absolute;
				right: 191upx;
			}

			.icon-y {
				width: 26upx;
				height: 26upx;
				position: absolute;
				right: 26upx;
			}

		}

	}

	.add-btn {
		margin: 50upx auto;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 80%;
		height: 73upx;
		background: #13C0A4;
		border-radius: 36upx;
		font-size: 23upx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: rgba(255, 255, 255, 1);
	}
</style>
